<html>
<head>
    <link rel="stylesheet" href="/root/static/bootstrap/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/root/static/bootstrap/bootstrap-table/bootstrap-table.min.css">
    <link rel="stylesheet" href="/root/static/font-awesome/css/font-awesome.min.css">
    <script src="/root/static/jquery/jquery.min.js"></script>
	<script src="/root/static/jquery/jquery.cookie.js"/>
    <script src="/root/static/bootstrap/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="/root/static/bootstrap/bootstrap-table/bootstrap-table.js"></script>
    <script src="/root/static/bootstrap/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
    <script src="/root/static/bootstrap/bootstrap-table/export/bootstrap-table-export.js"></script>
    <script src="/root/static/bootstrap/bootstrap-table/export/tableExport.js"></script>
    <script src="/root/static/layer/layer.js"></script>
    <script src="/root/static/bootstrap/bootstrap-valid/bootstrapValidator.min.js"></script>
    <script src="/root/static/bootstrap/bootstrap-valid/zh_CN.js"></script>
	<meta charset="utf-8"/>
</head>
<body>
<form class="form-horizontal" role="form" method="post" onsubmit="return doRegister();" id="loginForm">
    <div class="form-group">
        <label class="col-sm-3 control-label">邮箱</label>
        <div class="col-sm-3">
            <input type="text" class="form-control" name="email">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">密码</label>
        <div class="col-sm-3">
            <input type="text" class="form-control" name="password">
        </div>
    </div>
	<div class="form-group">
        <label class="col-sm-3 control-label">验证码</label>
        <div class="col-sm-3">
            <input type="text" class="form-control" name="code">
        </div>
		 <button type="button" class="btn btn-default" onclick="getCode();" id="codeBtn">获取</button>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-10">
            <button type="submit" class="btn btn-default">注册</button>
            
        </div>
    </div>
</form>
<script>
	function getCode(){
		var email = $("[name='email']").val();
		$.post("/api/code/create/" + email,function(data){
			layer.msg("获取成功",{icon:1});
			$("#codeBtn").attr("disabled", true);
			var maxtime = 60;
			var obj = setInterval(function(){
			var index = --maxtime;
			$("#codeBtn").text(index);
			if (maxtime==0) {
				 clearInterval(obj);
				 $("#codeBtn").text("获取");
				 $("#codeBtn").attr("disabled", false);
			}
			},1000); 
		});
	}
	function doRegister() {
		var obj = $("#loginForm").serializeArray();
        var param = {};
        $.each(obj, function(i, field){
            param[field.name]=field.value;
        });
        $.ajaxSetup({
            async: false,
			contentType: "application/json; charset=utf-8"
        });
		var register = false;
		$.post("/api/user/isRegister/" + param.email,function(res) {
			register = res.data;
		});
		if (register) {
			layer.msg("该用户已注册",{icon:2});
			return false;
		}
		$.post("/api/user/register", JSON.stringify(param),function(res) {
			console.info(res);
			if (res.success) {
			 $.cookie('cloud-token', res.data.token, {expires:7, path:'/'});
			 //console.info($.cookie('cloud-token'));
			location.href="/root/welcome.html";
			}
			else {
				layer.msg(res.msg,{icon:2});
			}
		},"json");
		
        return false;
	}
</script>
</body>
</html>